package com.kye.express.business.jd.search_list.search_result;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemJdSearchResultBinding;
import com.kye.model.bean.jd.shop.JDGoodsItemBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * 京东兑换-搜索-结果页面 ItemViewHolder
 * <p>
 * Created by SkyYu on 2018/1/29
 */
public class JDGoodsSearchResultViewBinder extends ItemViewBinder<JDGoodsItemBean, JDGoodsSearchResultViewBinder.ViewHolder> {

    private OnGoodsItemListener onItemListener;

    JDGoodsSearchResultViewBinder(OnGoodsItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_search_result, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDGoodsItemBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdSearchResultBinding binding;
        private JDGoodsItemBean item;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);

            RxView.clicks(binding.linearlayoutJdSearchResult)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onGoodsItemClicked(item);
                            }
                        }
                    });
        }

        public void bindData(JDGoodsItemBean item) {
            this.item = item;
            binding.setData(item);
        }
    }

    interface OnGoodsItemListener {
        void onGoodsItemClicked(JDGoodsItemBean item);
    }
}